<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="zip.constants.html">« 预定义常量</a></li>
      <li style="float: right;"><a href="class.ziparchive.html">ZipArchive »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.zip.html">Zip</a></li>
    <li>范例</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="zip.examples" class="chapter">
 <h1>范例</h1>

 <div class="example" id="example-826">
  <p><strong>示例 #1 创建一个 Zip 归档</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$zip&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ZipArchive</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"./test112.zip"</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">open</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ZIPARCHIVE</span><span style="color: #007700">::</span><span style="color: #0000BB">CREATE</span><span style="color: #007700">)!==</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #DD0000">"cannot&nbsp;open&nbsp;&lt;</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">&gt;\n"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addFromString</span><span style="color: #007700">(</span><span style="color: #DD0000">"testfilephp.txt"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"#1&nbsp;This&nbsp;is&nbsp;a&nbsp;test&nbsp;string&nbsp;added&nbsp;as&nbsp;testfilephp.txt.\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addFromString</span><span style="color: #007700">(</span><span style="color: #DD0000">"testfilephp2.txt"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"#2&nbsp;This&nbsp;is&nbsp;a&nbsp;test&nbsp;string&nbsp;added&nbsp;as&nbsp;testfilephp2.txt.\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addFile</span><span style="color: #007700">(</span><span style="color: #0000BB">$thisdir&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"/too.php"</span><span style="color: #007700">,</span><span style="color: #DD0000">"/testfromfile.php"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"numfiles:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">numFiles&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"status:"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">status&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <div class="example" id="example-827">
  <p><strong>示例 #2 输出文档细节和列表</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$za&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ZipArchive</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$za</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">open</span><span style="color: #007700">(</span><span style="color: #DD0000">'test_with_comment.zip'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$za</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$za</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"numFiles:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$za</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">numFiles&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"status:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$za</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">status&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"statusSys:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$za</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">statusSys&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"filename:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$za</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">filename&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"comment:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$za</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">comment&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />for&nbsp;(</span><span style="color: #0000BB">$i</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">$za</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">numFiles</span><span style="color: #007700">;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"index:&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$za</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">statIndex</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">));<br />}<br />echo&nbsp;</span><span style="color: #DD0000">"numFile:"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$za</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">numFiles&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <div class="example" id="example-828">
  <p><strong>示例 #3 Zip 流封装，读取一个 OpenOffice 文件的元信息</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$reader&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">XMLReader</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$reader</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">open</span><span style="color: #007700">(</span><span style="color: #DD0000">'zip://'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'/test.odt#meta.xml'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$odt_meta&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />while&nbsp;(</span><span style="color: #0000BB">$reader</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">read</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$reader</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">nodeType&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">XMLREADER</span><span style="color: #007700">::</span><span style="color: #0000BB">ELEMENT</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$elm&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$reader</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$reader</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">nodeType&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">XMLREADER</span><span style="color: #007700">::</span><span style="color: #0000BB">END_ELEMENT&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">$reader</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">'office:meta'</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$reader</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$odt_meta</span><span style="color: #007700">[</span><span style="color: #0000BB">$elm</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$reader</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$odt_meta</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <p class="para">
  此例使用了旧的 API（PHP 4），它打开了一个 ZIP 文件归档，读取归档里的每个文件，并输出文件内容。此例用到的
  <var class="filename">test2.zip</var> 文档是 ZZIPlib 源分布里测试文档中的一个。
 </p>
 <div class="example" id="example-829">
  <p><strong>示例 #4 Zip 使用范例</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$zip&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">zip_open</span><span style="color: #007700">(</span><span style="color: #DD0000">"/tmp/test2.zip"</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$zip</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$zip_entry&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">zip_read</span><span style="color: #007700">(</span><span style="color: #0000BB">$zip</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">zip_entry_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$zip_entry</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Actual&nbsp;Filesize:&nbsp;&nbsp;&nbsp;&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">zip_entry_filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$zip_entry</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Compressed&nbsp;Size:&nbsp;&nbsp;&nbsp;&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">zip_entry_compressedsize</span><span style="color: #007700">(</span><span style="color: #0000BB">$zip_entry</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Compression&nbsp;Method:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">zip_entry_compressionmethod</span><span style="color: #007700">(</span><span style="color: #0000BB">$zip_entry</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">zip_entry_open</span><span style="color: #007700">(</span><span style="color: #0000BB">$zip</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$zip_entry</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"File&nbsp;Contents:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$buf&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">zip_entry_read</span><span style="color: #007700">(</span><span style="color: #0000BB">$zip_entry</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">zip_entry_filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$zip_entry</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$buf</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">zip_entry_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$zip_entry</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">zip_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$zip</span><span style="color: #007700">);<br /><br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
</div>
</div></div></body></html>